﻿#include "conndlg.h"
#include "ui_conndlg.h"
#include<QSqlError>
#include<QSqlDatabase>
#include<QSqlQuery>
#include<QtSql>
#include<QDebug>
ConnDlg::ConnDlg(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
    QStringList drivers=QSqlDatabase::drivers();
    ui.comboDriver->addItems(drivers);
    connect(ui.comboDriver,SIGNAL(currentTextChanged(QString)),this,SLOT(driverChanged(const QString &)));
    ui.status_label->setText(tr("准备连接数据库"));
}

ConnDlg::~ConnDlg()
{
    delete &ui;
}

QString ConnDlg::driverName()const
{
    return ui.comboDriver->currentText();
}
QString ConnDlg::databaseName()const
{
    return ui.editDatabase->text();
}
QString ConnDlg::userName()const
{
    return ui.editUsername->text();
}
QString ConnDlg::password()const
{
    return ui.editPassword->text();
}
QString ConnDlg::hostName()const
{
     return ui.editHostname->text();
}
int ConnDlg::port()const
{
    return ui.portSpinBox->value();
}
QSqlError ConnDlg::addConnection(const QString &driver,const QString &dbName,const QString &host,const QString &user,const QString &passwd,int port)
{
    QSqlError err;
    QSqlDatabase db=QSqlDatabase::addDatabase(driver);
    db.setDatabaseName(dbName);
    db.setHostName(host);
    db.setPort(port);
    if(!db.open(user,passwd))
    {
        err=db.lastError();
    }
    return err;
}
void ConnDlg::creatDB()
{
    QSqlQuery query;
    query.exec("create table factory (id int primary key,manufactory varchar(40),address varchar(40))");
    query.exec(QObject::tr("insert into factory values(1,'一汽大众','长春')"));
    query.exec(QObject::tr("insert into factory values(2,'二汽神龙','武汉')"));
    query.exec(QObject::tr("insert into factory values(3,'上海大众','上海')"));

    query.exec("create table cars (carid int primary key,name varchar(50),factoryid int,year int,foreign key(factoryid) references factory)");
    query.exec(QObject::tr("insert into cars values(1,'奥迪A6',1,2005)"));
    query.exec(QObject::tr("insert into cars values(2,'毕加索',2,1999)"));
    query.exec(QObject::tr("insert into cars values(3,'帕萨特',3,2000)"));
    query.exec(QObject::tr("insert into cars values(4,'问届',2,3000)"));
}
void ConnDlg::addSqliteConnection()
{
    QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("databasefile.db");
    if(!db.open())
    {
        ui.status_label->setText(db.lastError().text());
        return;
    }
    ui.status_label->setText(tr("创建sqlite数据库成功"));
}

void ConnDlg::on_okButton_clicked()
{
    if(ui.comboDriver->currentText().isEmpty())
    {
        ui.status_label->setText(tr("请选择一个数据库驱动"));
        ui.comboDriver->setFocus();//设置焦点
    }else if(ui.comboDriver->currentText()=="QSQLITE")
    {
        addSqliteConnection();
        creatDB();
        accept();
    }else {
        QSqlError err=addConnection(driverName(),databaseName(),hostName(),userName(),password(),port());
        if(err.type()!=QSqlError::NoError)
        {
            ui.status_label->setText(err.text());
        }else{
            ui.status_label->setText(tr("连接数据库成功!"));
            accept();
        }
    }
}
void ConnDlg::driverChanged(const QString &text)
{
    if(text=="QSQLITE")
    {
        ui.editDatabase->setEnabled(false);
        ui.editHostname->setEnabled(false);
        ui.editPassword->setEnabled(false);
        ui.editUsername->setEnabled(false);
        ui.portSpinBox->setEnabled(false);
    } else {
        ui.editDatabase->setEnabled(true);
        ui.editHostname->setEnabled(true);
        ui.editPassword->setEnabled(true);
        ui.editUsername->setEnabled(true);
        ui.portSpinBox->setEnabled(true);
    }
}
